package com.example.project1.screen;

import java.io.Serializable;

import javax.microedition.khronos.opengles.GL10;

import com.example.project1.GameActivity;
import com.example.project1.main.Renderer;
import com.example.project1.sandbox.Simulation;
import com.example.project1.sandbox.SimulationListener;
/**
 * 
 * @author Ballach
 * instances renderer and simulation
 */
public class GameLoop implements GameScreen, SimulationListener, Serializable {

	public Simulation simulation;
	Renderer renderer;
	//SoundManager soundManager;

	public GameLoop(GL10 gl, GameActivity activity) {
		simulation = new Simulation();
		simulation.listener = this;
		renderer = new Renderer(gl, activity);
		//soundManager = new SoundManager(activity);
	}

	public GameLoop(GL10 gl, GameActivity activity, Simulation simulation) {
		this.simulation = simulation;
		this.simulation.listener = this;
		renderer = new Renderer(gl, activity);
		//soundManager = new SoundManager(activity);
	}
	/**
	 * 
	 * @param activity
	 * wait for playercontrol/accelerometer
	 * check if the screen was touched and shoot
	 */
	private void processInput(GameActivity activity) {
		if (activity.getAccelerationOnYAxis() < 0)
			simulation.moveShipLeft(activity.getDeltaTime(),
					Math.abs(activity.getAccelerationOnYAxis()) / 10);
		else
			simulation.moveShipRight(activity.getDeltaTime(),
					Math.abs(activity.getAccelerationOnYAxis()) / 10);

		if (activity.isTouched())
			simulation.shot();
	}

	@Override
	public void explosion() {
		//soundManager.playExplosionSound();

	}

	@Override
	public void shot() {
		//soundManager.playShotSound();

	}

	@Override
	public void update(GameActivity activity) {
		processInput(activity);
		simulation.update(activity.getDeltaTime());

	}

	@Override
	public void render(GL10 gl, GameActivity activity) {
		renderer.render(gl, activity, simulation);

	}

	@Override
	public boolean isDone() {
		return simulation.ship.lives == 0;
	}

	@Override
	public void dispose() {
		renderer.dispose();
		//soundManager.dispose();

	}

}
